﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MyLiveMesh.MVVMUtils;
using MyLiveMesh.Models;
using System.ServiceModel;

namespace MyLiveMesh.ViewModels
{
    public class LoginViewModel : ViewModelBase, INavigableViewModel    
    {
        private ICommand loginCommand = null;
        private ICommand registerCommand = null;
        private string login;
        private string password;

        public string Login { get { return login; } set { login = value; raisePropertyChanged("Login"); } }
        public string Password { get { return password; } set { password = value; raisePropertyChanged("Password"); } }

        public ICommand LoginCommand
        {
            get
            {
                if (loginCommand == null)
                    loginCommand = new RelayCommand<object>(new Action<object>((object o) =>
                {
                    Client.Current.connectAsync(login, password);
                }));
                return loginCommand;
            }
        }

        public ICommand RegisterCommand
        {
            get
            {
                if (registerCommand == null)
                    registerCommand = new RelayCommand<object>(new Action<object>((object o) =>
                    {
                        this.Navigator.goTo("/Views/RegisterPage.xaml");
                    }));
                return registerCommand;
            }
        }




        public LoginViewModel()
        {

            Client.Current.connectCompleted += new EventHandler<ServiceReference2.connectCompletedEventArgs>(Current_connectCompleted);
        }

        void Current_connectCompleted(object sender, ServiceReference2.connectCompletedEventArgs e)
        {
            if (e.Result)
                this.Navigator.goTo("/Views/DashboardPage.xaml");
            else
            {
                login = null;
                password = null;
            }
        }

        public INavigator Navigator{get; set;}
    }
}
